function checkAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = false ;
}

var currentPlaylist = null;
var currentLength = 0;
var currentItem = -1; 
var previousItem = -1; 
var currentMute = false; 
var currentVolume = 80; 
var currentPosition = 0; 
var currentState = 'NONE';
var currentLoaded = 0;
var currentRemain = 0;

var player = null;
function playerReady(thePlayer) {
	player = window.document[thePlayer.id];
	addListeners();
}


function addListeners() {
	if (player) { 
		player.addControllerListener("ITEM", "itemListener");
		player.addControllerListener("MUTE", "muteListener");

		player.addModelListener("LOADED", "loadedListener");
		player.addModelListener("STATE", "stateListener");
		player.addModelListener("TIME", "positionListener");

		player.addViewListener("VOLUME", "volumeListener");
	} else {
		setTimeout("addListeners()",100);
	}
}


function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	currentState = obj.newstate; 
	previousState = obj.oldstate; 

	var tmp = document.getElementById("stat");
	if (tmp) { 
		tmp.innerHTML = "current state: " + currentState + 
		"<br>previous state: " + previousState; 
	}
}


function positionListener(obj) { 
	currentPosition = obj.position; 
	var tmp = document.getElementById("tim");
	if (tmp) { tmp.innerHTML = "<b>Position:</b> " + currentPosition; }
}


function loadedListener(obj) { 
	currentLoaded = obj.loaded; 
	currentRemain = obj.total - currentLoaded;
	var tmp = document.getElementById("ldd");
	if (tmp) { 
		tmp.innerHTML = "<b>Bytes Loaded:</b> " + currentLoaded +
				" <b>Bytes Remaining:</b> " + currentRemain; 
	}
}


function volumeListener(obj) { 
	currentVolume = obj.percentage; 
	var tmp = document.getElementById("vol");
	if (tmp) { tmp.innerHTML = "Volume: " + currentVolume; }
}


function muteListener(obj) { 
	currentMute = obj.state; 
	var tmp = document.getElementById("mut");
	if (tmp) { tmp.innerHTML = "Mute: " + currentMute; }
}


function itemListener(obj) { 
	if (obj.index != currentItem) {
 		previousItem = currentItem;
		currentItem = obj.index;

		if (previousItem == -1) { getPlaylistData(); }

		var tmp = document.getElementById("itm");
		if (tmp) { 
			tmp.innerHTML = "<b>Current Item:</b> " + currentItem +
				" <b>previous Item:</b> " + previousItem;
		}

		var tmp = document.getElementById("item");
		if (tmp) { tmp.innerHTML = "item: " + currentItem; }

		var tmp = document.getElementById("pid"); 
		if (tmp) { 
			tmp.innerHTML = "(received from the player with the id: <i><b>" + obj.id + "</b></i>)"; 
		} 

		printItemData(currentItem);
	}
}



 function PopUp(url, width, height)
{
var l = (screen.width - width) / 2 - 10;
var t = (screen.height - height) /2 - 10;
var opts = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height + ',top=' + t + ' ,left=' + l;
var name = 'YouTubeVideo';
var comwin2 = window.open(url, name, opts);
comwin2.focus();
}

function printItemData(theIndex) {
	var plst = null;
	plst = player.getPlaylist();

	if (plst) {
		var txt = '';
		txt += '' + plst[theIndex].link + '';
		//txt += '<li><b>link: </b>' + plst[theIndex].file + '</li>';
		//txt += '<li><b>link: </b>' + plst[theIndex].image + '</li></ul>';
		newtxt = txt.replace('http://www.youtube.com/watch?v=', '<b><form style="display:inline;"><input onclick="javascript:this.select()" style="text-align:center;font-weight:bold;color:brown" type="text" value="');
		laidedelvideo = newtxt.replace("&feature=youtube_gdata", '"</input></form>');
		
		
		var tmp = document.getElementById("itmsDat");
		if (tmp) { tmp.innerHTML = laidedelvideo; }
	} 	
}


function getPlaylistData() { 
	var plst = null;
	plst = player.getPlaylist();

	if (plst) { 
		currentPlaylist = plst; 

	var txt = ''; 
	for(var i in currentPlaylist) { 
	txt += '<a href="#video" onclick="javascript:player.sendEvent(\'ITEM\',' + i + ')">' ;
	//txt += '<a target="_top" href="http://www.ymusicvideos.com/?s=' + currentPlaylist[i].title + '">' ;
	//txt += '<a target="_blank" href="http://www.ymusicvideos.com/?s=' + currentPlaylist[i].title + '">' ;
	txt += '<img width="120" height="90" style="margin:5px;vertical-align:middle;text-align:center;" src="' + currentPlaylist[i].image + '" title="' + i + ' ' + currentPlaylist[i].title + '" alt="' + i + '"/>';
	txt += '</a>';
	}
		var tmp = document.getElementById("plstDat");
		if (tmp) { tmp.innerHTML = txt; }
	}	
}


function loadCheckedPlaylistData() { 
	if (currentPlaylist) { 
		var j = 0; 
		var lst = new Array();
		for(var i in currentPlaylist) { 
			if(document.getElementById('cb' + i).checked) {
				lst[j] = {
					author:currentPlaylist[i].author,
					description:currentPlaylist[i].description,
					duration:currentPlaylist[i].duration,
					file:currentPlaylist[i].file,
					link:currentPlaylist[i].link,
					image:currentPlaylist[i].image,
					start:currentPlaylist[i].start,
					title:currentPlaylist[i].title,
					type:currentPlaylist[i].type
				}
				j++;
			}
		}
		if(lst.length > 0) { player.sendEvent('LOAD', lst); }
	}	
}


function getLength() { currentLength = player.getPlaylist().length; return(currentLength); };


function loadFile(theFile) { currentItem = -1; previousItem = -1; player.sendEvent('LOAD', theFile); };